
from HTMLParser2 import HTMLParser2

class WnParser ( HTMLParser2 ):
	h1 = []
	h2 = []
	h3 = []
	title = None
	description = None
	links = []
	stripped_text = "";
	ignore_data = False
	current_tag = None

	def cleanup(self):
		self.h1 = []
		self.h2 = []
		self.h3 = []
		self.title = None
		self.description = None
		self.links = []
		self.stripped_text = "";
		self.ignore_data = False
		self.current_tag = None		

	def handle_starttag(self, tag, attributes):
		if(tag.lower() in ["style", "script", "object", "embed", "meta", 'a', 'img', 'title']):
			self.ignore_data = True
		else:
			self.ignore_data = False

		if(tag.lower() in ["a", "h1", "h2", "h3", "title", "meta"]):
			if tag == 'meta':
				for name, value in attributes:
					if name == 'name' and value.lower() == 'description' and self.description == None:
						for name, value in attributes:
							if name.lower() == 'content':
								self.description = value
			self.current_tag = {
				"tag" : tag.lower(),
				"attrs" : attributes,
				"data" : "",
			}

		else:
			self.current_tag = None
	def error(self, data):
		print "ERROR"

	

	def handle_data(self, data):
		if self.ignore_data == False:
			self.stripped_text = self.stripped_text + ' ' + data.strip(". ,!@#$%^&*()_+-=? \t\n\r")
		if(self.current_tag != None):
			self.current_tag["data"] = data
			
			if(self.current_tag['tag'] == "h1"):
				self.h1.append(data)
			if(self.current_tag['tag'] == "h2"):
				self.h2.append(data)
			if(self.current_tag['tag'] == "h3"):
				self.h3.append(data)
			if(self.current_tag['tag'] == "a"):
				self.links.append(data)
			if(self.current_tag['tag'] == "meta"):
				for name, value in self.current_tag['attrs']:
					if name == 'name' and value.lower() == 'description' and self.description == None:
						for name, value in self.current_tag['attrs']:
							if name.lower() == 'content':
								self.description = value

	def print_vars( self ):
		print "desc: %s" % self.description
		print "title: %s" % self.title
		for i in self.h1:
			print "h1: '%s'" % i
		for i in self.h2:
			print "h2: '%s'" % i
		for i in self.h3:
			print "h3: '%s'" % i
		for i in self.links:
			print "link: '%s'" % i

	def getDescription(self):
		if self.description != None:
			return self.description
		return ""
	def getTitle(self):
		if self.title != None:
			return self.title
		return ""
	def getH1(self):
		if len(self.h1)>0:
			return self.h1[0]
		return "";
	def getH2_1(self):
		if len(self.h2)>0:
			return self.h2[0]
		return ""
	def getH2_2(self):
		if len(self.h2)>1:
			return self.h2[1]
		return ""
	def getLinks(self):
		links_text = "";
		for i in self.links:
			links_text+=i
			links_text+="|||"
		return links_text